from config import *
import fbs
import gys


class XMB:
    def __init__(self, name, warehouse, price_out):
        self.name = name
        self.warehouse = warehouse  # 项目部的仓库名字
        self.price_out = price_out  # 项目部出库单价

        self.purchase_form = ""  # 采购订单
        self.put_in_form = ""  # 项目部 入库单
        self.add_fld = ''  # 项目部 发料单

        # self.purchase_form = "CGJH-20240502-8196"  # 采购订单
        # self.put_in_form = "RK-20240502-8200"  # 项目部 入库单
        # self.add_fld = 'CKXQ-20240502-8202'  # 项目部 发料单

    # 切换角色
    def into(self):
        sleep(1)
        xpath("//span[contains(text(), '好')]")  # 点击 右上角的 “上午好或下午好”
        xpath("//li[contains(text(), '切换账号')]")  # 点击 切换账号
        xpath("//div[contains(text(), '%s')]" % self.name)  # 点击 名字 进行切换 账号
        sleep(2)

    # 项目部专属导航搜索定位 2级目录
    def navbar(self, text1, text2):
        sleep(2)
        query('//*[@id="app"]/div/section/section/aside/ul/div/*/div', text1)
        query('//*[@id="app"]/div/section/section/aside/ul/div/*/ul/a/li', text2)

    # 新增采购单  暂时只支持 普通材料采购  传入供应商 和分包商
    def add_purchase_form(self, gys, fbs):
        sleep(1)
        query('//*[@id="app"]/div/section/section/aside/ul/div/*/ul/a/li', "普通材料采购")
        query('//*[@id="app"]/div/section/section/main/section/*/*/*/*', "新增采购计划单"), sleep(1)  # 停止一下 可能找不到 报错：1
        # 选择供应商
        xpath(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/form/div/div[2]/div/div/div/div/input')
        query('/html/body/*/*/*/ul/li', gys.name)
        # 物资申请单
        xpath(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/form/div/div[3]/div/div/div/div[1]/input'), sleep(1)  # 一次报错  等反映 延长
        query('/html/body/*/*/*/ul/*', fbs.application_form)
        xpath_input(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/form/div/div[5]/div/div/div/input',
            location)  # 地址
        xpath_input(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/form/div/div[6]/div/div/div/textarea',
            '第六组')  # 备注
        query('//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/div[2]/*',
              '提交单据')  # 提交

        # 保存 采购单
        self.purchase_form = xpath_text(
            '//*[@id="app"]/div/section/section/main/section/div/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[2]/div')
        print('采购订单：' + self.purchase_form)
        sleep(1)

    # 项目部出库
    def put_out(self, fbs):
        xpath('//*[@id="app"]/div/section/section/main/section/div/*/ul/*/div[contains(text(), "出入库管理")]')
        sleep(0.3)
        query("//label[1]", "出库")
        query('//*[@id="app"]/div/section/section/main/section/div/*/*/*/*/*/*/*/*', "新增出库单")
        # 出库对象
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[7]/div[2]/div/div[2]/div/div[1]/div[2]/div[2]/div/div/input')
        query('//ul/*', "分包商")
        # 出库对象
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[7]/div[2]/div/div[2]/div/div[1]/div[3]/div[2]/div/div/input')
        query('/html/body/*/*/*/ul/*', self.warehouse)
        # 选择分包单位
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[7]/div[2]/div/div[2]/div/div[1]/div[4]/div[2]/div/div/input')
        query('/html/body/*/*/*/ul/*', fbs.name)
        # 关联发料单
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[7]/div[2]/div/div[2]/div/div[1]/div[5]/div[2]/div/div/input')
        query('/html/body/*/*/*/ul/*', self.add_fld)
        # 出库单价 清空再点击
        driver.find_element_by_xpath(
            "/html/body/div[1]/div/section/section/main/section/div/div[7]/div[2]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[8]/div/div/input").clear()
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[7]/div[2]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[8]/div/div/input',
            self.price_out)
        # 出库数量
        driver.find_element_by_xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[7]/div[2]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[9]/div/div/input').clear()
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[7]/div[2]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[9]/div/div/input',
            count)
        sleep(0.5)
        query('//*[@id="app"]/div/section/section/main/section/div/*/*/*/*/*/*', "确定出库")
        sleep(5)
        # ? 问题点二  右上角X  报错+
        driver.find_element_by_xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[7]/div[1]/div[1]/div[1]/button/i').click()
        sleep(2)

    def put_in(self):  # 入库
        sleep(1)
        xpath('//*[@id="app"]/div/section/section/main/section/div/*/ul/*/div[contains(text(), "出入库管理")]')
        query("//label[2]", "入库")  # 右上角
        query('/html/body/div[1]/div/section/section/main/section/div/*/*/div/*/div/*/div/*/table/tbody/*/*/div/*',
              "处理")
        # 保存单号
        self.put_in_form = xpath_text(
            '/html/body/div[1]/div/section/section/main/section/div/*/div[1]/div/div[2]/div/div[3]/div/div[3]/table/tbody/tr[1]/td[2]/div')
        sleep(0.5)  # 留给系统缓存时间，防止保存上一个
        # 入库仓库
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/*/div[3]/div/div[2]/div/div[1]/div[1]/div[3]/div[2]/div/div/input')
        query('/html/body/*/*/*/ul/*', self.warehouse)
        print("项目部入库单：" + self.put_in_form)
        sleep(0.5)  # 一次保存 无法点击签收入库 加时间 循环第六次错误
        query('/html/body/div[1]/div/section/section/main/section/div/*/*/div/*/div/*', "签收入库")
        sleep(3)  # 防止找不到
        xpath('/html/body/div[1]/div/section/section/main/section/div/div[7]/div[1]/div[1]/div[1]/button/i')  # 右上角X

    def examine(self):
        # 物料检测
        sleep(1.5)  # 检验经常报错 所以强制增加时间
        # query("//button[2]", "检测")
        # 有问题的地方之一 检测
        driver.find_element_by_xpath(
            '/html/body/div/div/section/section/main/section/div/div[2]/div/div[3]/table/tbody/tr/td[8]/div/button').click()

        xpath_input(
            '//*[@id="app"]/div/section/section/main/section/div/div[4]/div/div[2]/div/form/div/div[4]/div/div/div/input',
            "炸薯条")
        xpath_input(
            '//*[@id="app"]/div/section/section/main/section/div/div[4]/div/div[2]/div/form/div/div[7]/div/div/div/textarea',
            '检测及格')
        xpath(
            '//*[@id="app"]/div/section/section/main/section/div/div[4]/*/div[2]/div/div/div/div[3]/table/tbody/tr/td[4]/div/div/div/input')  # 检测结果
        # xpath('//*[@id="app"]/div/section/section/main/section/div/div[4]/div[1]/div[2]/div/div/div/div[3]/table/tbody/tr/td[4]/div/div/div/input') # 检测结果

        query('/html/body/*/*/*/ul/*', "合格")
        query('//*[@id="app"]/div/section/section/main/section/div/*/div/*/div/*', "保存")

    # 项目部新增发料单
    def add_send_form(self, fbs):
        sleep(2)
        query('/html/body/div/div/section/section/main/section/div/*/form/*', "新增发料单")  # 新增发料单
        xpath(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div[1]/div[2]/div/form/div/div[2]/div/div/div/div/input')  # 出库对象
        xpath('/html/body/*/*/*/ul/*/span[contains(text(), "分包商")]')  # 选择出库对象
        xpath(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/form/div/div[3]/div/div/div/div/input')  # 选择分包单位
        # xpath('/html/body/div[5]/div[1]/div[1]/ul/li/span[contains(text(), "%s")]' %fbs)
        query('/html/body/*/*/*/ul/li', fbs.name)
        # 出库仓库
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[3]/div/div[2]/div/form/div/div[4]/div/div/div/div/input')
        query('/html/body/*/*/*/ul/*', self.warehouse)
        # 关联入库单
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[3]/div/div[2]/div/form/div/div[8]/div/div/div/div/div[1]/input')
        query('/html/body/*/*/*/ul/*', self.put_in_form)
        # 关联申请单
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[3]/div/div[2]/div/form/div/div[9]/div/div/div/div[1]/input')
        query('/html/body/*/*/*/ul/*', fbs.application_form)
        # 收货地址
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[3]/div/div[2]/div/form/div/div[10]/div/div/div/input',
            location)
        # 确认
        query('/html/body/div[1]/div/section/section/main/section/div/div[3]/div/div[3]/div/*', "确认单据")
        # 保存发料单信息
        self.add_fld = xpath_text(
            "/html/body/div/div/section/section/main/section/div/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[2]/div")
        print("项目部发料单：" + self.add_fld)
